import { Injectable, Logger, HttpService } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { NewsService } from '../news/news.service';
import { Statistics } from '../news/interface/statistics.interface';
@Injectable()
export class TasksPuppeteerService {
  private readonly logger = new Logger(TasksPuppeteerService.name);
  constructor(
    private readonly newsService: NewsService,
    private readonly httpService: HttpService,
  ) {}

  @Cron('0 9-23/2 * * *')
  async handleCron() {
    this.logger.debug('开始执行爬虫任务');
    await this.run();
    this.logger.debug('结束执行爬虫任务');
  }

  async run() {
    const { areaStat, statisticsService } = await this.newsService.puppeteer();
    const guangdong = areaStat.find(v => v.provinceName === '广东省');
    try {
      await this.httpService
        .post('http://localhost:3008/wechat/yqarea', {
          china: statisticsService,
          guangdong,
        })
        .toPromise();
    } catch (e) {
      this.logger.error(e);
    }
    return {
      china: statisticsService,
      guangdong,
    };
  }
}
